import itertools
import pyzipper
import concurrent.futures

filename = "D:\\BaiduNetdiskDownload\\2023\\2.zip"
size = 4
chars = "1234567890abcdefghijklmnopqrstuvwsyzABCDEFGHIJKLMNOPQRSTUVWSYZ!#$%&'()*+,-./:;<=>?@[]^_{|}~`"

def extract_zip(zip_file, password):
    try:
        # 打开加密的ZIP文件
        with pyzipper.AESZipFile(zip_file) as zf:
            # 尝试使用密码打开
            zf.pwd = password.encode('utf-8')

            # 解压所有文件
            zf.extractall()
            print("解压成功！密码是:", password)
            return password
    except RuntimeError as e:
        # 密码错误时的处理
        return None
    except Exception as e1:
        # 其他错误
        return None

def generate_passwords(chars, size):
    return ("".join(c) for c in itertools.permutations(chars, size))

def try_passwords(zip_file, passwords):
    with concurrent.futures.ThreadPoolExecutor(max_workers=4) as executor:
        future_to_password = {executor.submit(extract_zip, zip_file, password): password for password in passwords}
        for future in concurrent.futures.as_completed(future_to_password):
            result = future.result()
            if result:
                print(f"找到正确的密码: {result}")
                return result
            else:
                print(f"密码不正确: {future_to_password[future]}")

# 生成所有可能的密码组合
passwords = generate_passwords(chars, size)

# 开始并行检查密码
result = try_passwords(filename, passwords)
if result:
    print(f"成功找到密码: {result}")
else:
    print("未找到正确的密码。")
